/**
 * DEPENDENCIAS:
 * nenhuma
 * 
 * FUNCIONALIDADE:
 * Funcoes que complementam a biblioteca de validacao 
 */

/*
 checa o PIS/PASEP
 */
function checkPISPASEP(field, rules, i, options){
	
	var ftap="3298765432", total=0, i, resto=0, strResto="";
	var numPIS = field.val();
	numPIS = numPIS.replace(/\.|-|\//gi,''); // elimina .(ponto), -(hifem) e /(barra)
	for(i=0;i<=9;i++){
		resultado = (numPIS.slice(i,i+1))*(ftap.slice(i,i+1));
		total=total+resultado;
	} 
	resto = (total % 11);
	if (resto != 0){
		resto=11-resto;
	}
	if (resto==10 || resto==11){
		strResto=resto+"";
		resto = strResto.slice(1,2);
	}	
	if (resto!=(numPIS.slice(10,11)))
		return options.allrules.pispasep.alertText;
}

/*
checa o CPF
*/
function checkCPF(field, rules, i, options){
        var cpf = field.val();
        cpf = cpf.replace(/\.|-|\//gi,''); // elimina .(ponto), -(hifem) e /(barra)
        if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999")
                return options.allrules.cpf.alertText;
        add = 0;
        for (i=0; i < 9; i ++) add += parseInt(cpf.charAt(i)) * (10 - i);
        rev = 11 - (add % 11);
        if (rev == 10 || rev == 11) rev = 0;
        if (rev != parseInt(cpf.charAt(9))) return options.allrules.cpf.alertText;
        add = 0;
        for (i = 0; i < 10; i ++) add += parseInt(cpf.charAt(i)) * (11 - i);
        rev = 11 - (add % 11);
        if (rev == 10 || rev == 11) rev = 0;
        if (rev != parseInt(cpf.charAt(10))) return options.allrules.cpf.alertText;
}

/*
checa o CNPJ
*/
function checkCNPJ(field, rules, i, options){
    var cnpj=field.val();
	cnpj=cnpj.replace(/[\.\/\-]/g,'');
	var multipliers='543298765432';
	var sum=0;
	var str='';
	for (var n=0;n<12;n++){
	sum+=(parseInt(cnpj.substr(n,1))*parseInt(multipliers.substr(n,1)));
	str+=cnpj.substr(n,1);
	}
	var dig1=parseInt(sum%11);
	dig1=(dig1<2?0:11-dig1);
	str+=String(dig1);
	multipliers='6'+multipliers;
	sum=0;
	for (var n=0;n<13;n++) sum+=(parseInt(str.substr(n,1))*parseInt(multipliers.substr(n,1)));
	var dig2=parseInt(sum%11);
	dig2=(dig2<2?0:11-dig2);
	if (cnpj.substr(-2,2)!=(String(dig1)+String(dig2))) return options.allrules.cnpj.alertText;
}